package com.internetCafes.spms.web.sys.service.impl;

import com.internetCafes.spms.web.sys.mapper.RoleInfoMapper;
import com.internetCafes.spms.web.sys.model.RoleInfo;
import com.internetCafes.spms.web.sys.service.RoleDeptInfoService;
import com.internetCafes.spms.core.annotation.DataFilter;
import com.internetCafes.spms.web.sys.service.RoleInfoService;
import com.internetCafes.spms.web.sys.service.RoleMenuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;



/**
 * 角色
 * 
 * @author Sunny
 * @email rekeeper2011@hotmail.com
 * @date 2016年9月18日 上午9:45:12
 */
@Service("sysRoleService")
public class RoleInfoServiceImpl implements RoleInfoService {
	@Autowired
	private RoleInfoMapper roleInfoMapper;
	@Autowired
	private RoleMenuInfoService roleMenuInfoService;
	@Autowired
	private RoleDeptInfoService roleDeptInfoService;

	@Override
	public RoleInfo queryObject(Long roleId) {
		return roleInfoMapper.queryObject(roleId);
	}

	@Override
	@DataFilter(tableAlias = "r", user = false)
	public List<RoleInfo> queryList(Map<String, Object> map) {
		return roleInfoMapper.queryList(map);
	}

	@Override
	@DataFilter(tableAlias = "r", user = false)
	public int queryTotal(Map<String, Object> map) {
		return roleInfoMapper.queryTotal(map);
	}

	@Override
	@Transactional
	public void save(RoleInfo role) {
		role.setCreateTime(new Date());
		roleInfoMapper.save(role);
		
		//保存角色与菜单关系
		roleMenuInfoService.saveOrUpdate(role.getId(), role.getMenuIdList());

		//保存角色与部门关系
		roleDeptInfoService.saveOrUpdate(role.getId(), role.getDeptList());
	}

	@Override
	@Transactional
	public void update(RoleInfo role) {
		roleInfoMapper.update(role);
		
		//更新角色与菜单关系
		roleMenuInfoService.saveOrUpdate(role.getId(), role.getMenuIdList());

		//保存角色与部门关系
		roleDeptInfoService.saveOrUpdate(role.getId(), role.getDeptList());
	}

	@Override
	@Transactional
	public void deleteBatch(Long[] roleIds) {
		roleInfoMapper.deleteBatch(roleIds);
	}

}
